package com.example.mybaiduweishi.activity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;

import com.example.mybaiduweishi.MyApplication;
import com.example.mybaiduweishi.R;
import com.example.mybaiduweishi.activity.base.BaseActivity;
import com.example.mybaiduweishi.fragments.SelectContactFragment;
import com.example.mybaiduweishi.fragments.ShowContactsFragment;
import com.example.mybaiduweishi.vo.Contacts;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class InsertContactsActivity extends BaseActivity {

    @BindView(R.id.edit_select)
    EditText editText;
    List<Contacts> list;
    List<Contacts> searchList;
    private Boolean flag = true;
    ShowContactsFragment showContactsFragment;    //第一个碎片
    SelectContactFragment selectContactFragment;    //第二个碎片

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public int getContentXML() {
        return R.layout.activity_insert_contacts;
    }

    @Override
    public void initData() {
        //由内容提供者查询联系人
        list = MyApplication.getList();
        searchList = new ArrayList<Contacts>();
        if (showContactsFragment == null) {
            showContactsFragment = new ShowContactsFragment();
        }
        getFragmentManager().beginTransaction()
                .replace(R.id.f1_show_contact, showContactsFragment)
                .addToBackStack("mobile")
                .commit();
    }

    /**
     * 处理回退异常
     */
    @Override
    public void onBackPressed() {
        super.onBackPressed();
        if (getFragmentManager().getBackStackEntryCount() > 1) {
            getFragmentManager().popBackStack();
        } else {
            finish();
        }
    }

    @Override
    public void initView() {
        /**
         * RefreshListByEditText
         */
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                /**
                 * 替换第一个Fragemnt
                 */
                if (flag) {
                    searchFragment();
                    flag = false;
                }
            }

            @Override
            public void onTextChanged(final CharSequence s, int start, int before, int count) {
                s.toString().replace(" ", "").replace("-", "");
                searchList.clear();
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        for (int i = 0; i < list.size(); i++) {
                            Contacts c = list.get(i);
                            if (c.getName().contains(s) || c.getTelnumber().toString().replace(" ", "").replace("-", "").contains(s)) {
                                searchList.add(c);
                            }
                        }
                        //循环完毕以后 更新第二个碎片
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                selectContactFragment.getSelectContactList(searchList);
                            }
                        });

                    }
                }).start();
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (editText.getText().length() == 0) {
//                    onBackPressed();
                    if (getFragmentManager().getBackStackEntryCount() > 1) {
                        getFragmentManager().popBackStack();
                    }
                    flag = true;
                }
            }
        });
    }

    public void searchFragment() {
        if (selectContactFragment == null) {
            selectContactFragment = new SelectContactFragment();
        }
        getFragmentManager().beginTransaction()
                .hide(showContactsFragment)
                .add(R.id.f1_show_contact, selectContactFragment)
                .addToBackStack("contacts").commit();
    }

    @OnClick(R.id.img_inser_back)
    public void onViewClicked() {
        finish();
    }


}

